package com.bdqn.tour.servlet.Hqf;

import javax.servlet.http.HttpServlet;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONArray;
import com.bdqn.tour.pojo.Scenic;
import com.bdqn.tour.service.HqfquerynameService;
import com.bdqn.tour.service.impl.hqfquerynameServiceImpl;

@WebServlet("/queryName")
public class HqfqueryNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
       req.setCharacterEncoding("utf-8");
       resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        // 获取请求参数
        String name = req.getParameter("scenic_name");
        //调用业务层 hqfquerynameService逻辑
        HqfquerynameService q = new hqfquerynameServiceImpl();
        //调用方法存入Scenic集合中
        List<Scenic> scenicList = q.getScenic(name);

        if (!scenicList.isEmpty()) {
            //从scenicList列表中获取第一个数据赋值给变量scenic
            Scenic scenic = scenicList.get(0);
            req.setAttribute("scenic_name", scenic.getScenic_name());
            req.setAttribute("scenic_address", scenic.getScenic_address());
            req.setAttribute("scenic_price", scenic.getPrice());
        }
         // 将结果转换为 JSON 并写入响应
        PrintWriter out = resp.getWriter();
        out.write(JSONArray.toJSONString(scenicList));
        out.flush();

    }
}
